/*
 * @Author: your name
 * @Date: 2022-02-28 21:03:56
 * @LastEditTime: 2022-02-28 23:24:06
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \test\websocket_server.h
 */

#pragma once
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
#include <thread>
#include <map>
#include <mutex>
#include <functional>

typedef websocketpp::server<websocketpp::config::asio> WsServer;


using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef WsServer::message_ptr message_ptr;


class WebsocketServer {
public:
    WebsocketServer(std::string server,uint32_t port);
    friend void on_new_connection(WebsocketServer* s, websocketpp::connection_hdl hdl);
    friend void on_message(WebsocketServer* s, websocketpp::connection_hdl hdl, message_ptr msg);
    friend void on_connection_close(WebsocketServer* s, websocketpp::connection_hdl hdl);
    int SendData(uint32_t,const char *data,int len,websocketpp::frame::opcode::value);
    int SendDataAllClient(const char *data,int len,websocketpp::frame::opcode::value);

private:
    std::string m_url;  // 地址
    uint32_t m_port;  // url
    WsServer *m_server;
    std::thread *m_thread;
    std::mutex m_mutex;
    std::map<uint32_t,websocketpp::connection_hdl> m_clients;
};


